import java.util.List;

public class MyLinkedList {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode tail;

    /**
     * 无头双向链表实现
     * @param data
     */
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        //空链表
        if(this.head == null){
            this.head = node;
            this.tail = node;
        }else {
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        //空链表
        if(this.head == null){
            this.head = node;
            this.tail = node;
        }else {
            this.tail.next = node;
            node.prev = this.tail;
            this.tail = node;
        }
    }

    /**
     * 任意位置插入,第一个数据节点为0号下标
     * @param index
     * 要对index位置判断是否合法
     * index在特殊位置 -头插和尾插
     * @param data
     * @return
     */
    public boolean addIndex(int index,int data){
        if(index<0 || size()<index){
            System.out.println("index位置不合法");
            return false;
        }
        if(index == 0){
            addFirst(data);
            return true;
        }
        if(index == size()){
            addLast(data);
            return true;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndexListNode(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
        return false;
    }
    public ListNode findIndexListNode(int index){
        ListNode cur = this.head;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur= cur.next;
        }
        return false;
    }
    /**
     * 删除第一次出现关键字为key的节点
     * 可能会遇到的特殊情况
     *    1.删除的节点在头或者尾
     *    2.只有一个结点
     * @param key
     */
    public void remove(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                if(cur==head){
                    if(cur.next==null){
                        this.head = null;
                        this.tail = null;
                        return;
                    }
                    this.head = head.next;
                    this.head.prev = null;
                }else{
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else{
                        this.tail = cur.prev;
                    }
                }
                return;
            }else{
                cur = cur.next;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                if(cur==head){
                    if(cur.next==null){
                        this.head = null;
                        this.tail = null;
                        return;
                    }
                    this.head = head.next;
                    this.head.prev = null;
                }else{
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else{
                        this.tail = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        //error
        this.head = null;
        this.tail = null;
       /* ListNode cur= this.head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        this.tail = null;*/
    }

    // -----------面试Top101---------------

    // 删除有序链表中重复的元素-I
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        // 1. 空链表
        if (head == null) {
            return null;
        }
        ListNode cur = head;
        while(cur.next != null){
            if(cur.val == cur.next.val){
                cur.next = cur.next.next;
            }else{
                cur = cur.next;
            }
        }
        return head;
    }
}

